package com.zego.wawaji.ui.center.question;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.zego.wawaji.R;
import com.zego.wawaji.base.album.ImgSelectUtil;
import com.zego.wawaji.base.album.help.AlbumHelper;
import com.zego.wawaji.base.media.MediaConstant;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.toast.PToast;
import com.noble.library.file.FileUtil;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.bean.UpLoadResult;
import com.zego.wawaji.ui.dialog.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 提交问题
 * Created by Su on 2018/4/11.
 */
public class QuestionAddAct extends BaseAppCompatActivity implements View.OnClickListener, ImgSelectUtil.OnChooseCompleteListener, RequestComplete {
    private EditText ed_phone, ed_content;
    private ImageView iv_current, iv_01, iv_02, iv_03, iv_04;
    private List<String> imageList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question_add_act);
        initView();
    }

    private void initView() {
        setBackView();
        setTitle("添加问题");

        ed_phone = (EditText) findViewById(R.id.ed_phone);
        ed_content = (EditText) findViewById(R.id.ed_content);
        iv_01 = (ImageView) findViewById(R.id.iv_01);
        iv_02 = (ImageView) findViewById(R.id.iv_02);
        iv_03 = (ImageView) findViewById(R.id.iv_03);
        iv_04 = (ImageView) findViewById(R.id.iv_04);
        iv_01.setOnClickListener(this);
        iv_02.setOnClickListener(this);
        iv_03.setOnClickListener(this);
        iv_04.setOnClickListener(this);
        findViewById(R.id.btn_submit).setOnClickListener(this);
    }

    private void submit() {
        String phone = ed_phone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            PToast.showShort("请输入联系手机号");
            return;
        }

        String content = ed_content.getText().toString();
        if (TextUtils.isEmpty(content)) {
            PToast.showShort("请输入问题内容");
            return;
        }

        ModuleMgr.getWawaMgr().reqQuestionAdd(phone, content, JSON.toJSONString(imageList), this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_01:
                picUpload(iv_01);
                break;
            case R.id.iv_02:
                picUpload(iv_02);
                break;
            case R.id.iv_03:
                picUpload(iv_03);
                break;
            case R.id.iv_04:
                picUpload(iv_04);
                break;
            case R.id.btn_submit:
                submit();
                break;
        }
    }

    private void picUpload(ImageView iv) {
        iv_current = iv;
        AlbumHelper.getInstance().setCropChannel(2);
        ImgSelectUtil.getInstance().pickPhotoGallery(this, this);
    }

    @Override
    public void onComplete(final String... path) {
        if (path == null || path.length == 0 || TextUtils.isEmpty(path[0])) {
            return;
        }
        LoadingDialog.show(this, "文件上传中");
        if (FileUtil.isExist(path[0])) {
            ModuleMgr.getMediaMgr().sendHttpFile(MediaConstant.UPLOAD_FEED_BACK, path[0], response -> {
                LoadingDialog.closeLoadingDialog();

                if (!response.isOk()) {
                    PToast.showShort(response.getMsg());
                    return;
                }
                ImageLoader.loadCenterCrop(QuestionAddAct.this, path[0], iv_current);
                UpLoadResult result = (UpLoadResult) response.getBaseData();
                imageList.add(result.getFile_s_path());
            });
        } else {
            LoadingDialog.closeLoadingDialog();
            PToast.showShort("图片地址无效");
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqQuestionAdd) {
            PToast.showShort(response.getMsg());
            if (response.isOk()) {
                finish();
            }
        }
    }
}
